package com.aspire.moss.portal.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;

import com.aspire.moss.portal.R;

public class CellLayout extends ViewGroup {

	private static final String TAG = "CellLayout";
	private int mWidth = 0;//屏幕的宽度
	private int mHeight ;
	
	float clos;
	float rows;
	
	float paddingLeft;
	float paddingRight;
	float paddingTop;
	float paddingBottom;
	
	Context mContext ;
	
	WorkspaceAdapter adapter;
	
	public void setAdapter(WorkspaceAdapter adapter) {
		this.adapter = adapter;
	}

	public CellInfo info = new CellInfo();
	
	public CellLayout(Context context, AttributeSet attrs){
		this(context, attrs, 0);
	}
	
	public CellLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout);
		clos = a.getInt(R.styleable.CellLayout_clos, 4);
		rows = a.getInt(R.styleable.CellLayout_rows, 4);
		paddingLeft = a.getDimension(R.styleable.CellLayout_paddingLeft, 0);
		paddingRight = a.getDimension(R.styleable.CellLayout_paddingRight, 0);
		paddingTop = a.getDimension(R.styleable.CellLayout_paddingTop, 0);
		paddingBottom = a.getDimension(R.styleable.CellLayout_paddingBottom, 0);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = MeasureSpec.getSize(widthMeasureSpec);
		mHeight = MeasureSpec.getSize(heightMeasureSpec);
		int mpmWidth = (int) (mWidth - paddingLeft - paddingRight);
		int mpmHeight = (int) (mHeight - paddingTop - paddingBottom);
		
		int cellWidth = (int) (mpmWidth / clos)  ;
		int cellHeight = (int) (mpmHeight / rows );
		View child = null;
		for(int i =  0 ; i < getChildCount() ; i++){
			child = getChildAt(i);
			int mw = MeasureSpec.makeMeasureSpec(cellWidth, MeasureSpec.EXACTLY);
			int mh = MeasureSpec.makeMeasureSpec(cellHeight, MeasureSpec.EXACTLY);
			child.measure(mw, mh);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log.e(TAG , "onTouchEvent");
		return super.onTouchEvent(event);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int mpmWidth = (int) (mWidth - paddingLeft - paddingRight);
		int mpmHeight = (int) (mHeight - paddingTop - paddingBottom);
		
		int cellWidth = (int) (mpmWidth / clos)  ;
		int cellHeight = (int) (mpmHeight / rows );
		View child = null;
		AppInfo info = null;
//		LayoutParams layoutParams = null;
		int cellX = 0;
		int cellY = 0;
		int left = 0;
		int top = 0;
		for(int i = 0 ; i < getChildCount() ; i ++){
			child = getChildAt(i);
			info = (AppInfo) child.getTag();
			info.mHeight = cellHeight;
			info.mWidth = cellWidth;
			cellX = info.cellX;
			cellY = info.cellY;
			left = (int) ((int) ((cellX) * cellWidth) + paddingLeft);
			top = (int) ((int) ((cellY) * cellHeight) + paddingTop);
			child.layout(left , top , left+ cellWidth, top + cellHeight);
		}

		
	}
	
//	@Override
//	public boolean dispatchTouchEvent(MotionEvent ev) {
////		int e = ev.getAction();
////		Log.e(TAG, ""+e);
////		boolean con = super.dispatchTouchEvent(ev);
////		switch (e) {
////		case MotionEvent.ACTION_MOVE:
////			return false;
////		case MotionEvent.ACTION_UP:
////			return false;
////		default:
////			break;
////		}
////		return con;
//	}
	
	 @Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
//		drawLine(canvas);
	}
	 
	 /**
	  * 是否还有空位
	  * @return
	  */
	public boolean isEmty(){
		 return getChildCount() <= clos * rows ? true : false;
		
	}
	 
	private void drawLine(Canvas canvas){
		
		int mpmWidth = (int) (mWidth - paddingLeft - paddingRight);
		int mpmHeight = (int) (mHeight - paddingTop - paddingBottom);
		
		int cellWidth = (int) (mpmWidth / clos)  ;
		int cellHeight = (int) (mpmHeight / rows );
		Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
//		
//		paint.setStyle(Style.FILL);
//
//		paint.setColor(Color.YELLOW);
//		RectF r = new RectF(paddingLeft, paddingTop, mWidth- paddingRight, mHeight - paddingBottom);
//		canvas.drawRect(r , paint);
//		canvas.save();
		paint.setColor(Color.BLUE);
		for(int i = 1 ; i <= clos ; i ++){
		canvas.drawLine((i * cellWidth) +paddingLeft, paddingTop, (i * cellWidth)+paddingLeft , mHeight -paddingBottom, paint);
	}
	
	for(int i = 1 ; i <= rows ; i ++){
		canvas.drawLine(paddingLeft, (i * cellHeight) + paddingTop ,mWidth - paddingRight ,(i * cellHeight) + paddingTop , paint);
	}
	canvas.save();
	}
	
	final class CellInfo extends ItemInfo {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		
		
	}

	public static final class LayoutParams extends MarginLayoutParams{

		public int cellX;
		
		public int cellY;
		
		public int mWidth;
		
		public int mHeight;
		
		public LayoutParams(Context arg0) {
			super(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		
		public LayoutParams(Context arg0, int cellX , int cellY , int mWidth , int mHeight ) {
			super(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			this.cellX = cellX;
			this.cellY = cellY;
			this.mWidth = mWidth;
			this.mHeight = mHeight;
		}
		
		
	}
	
	interface ContextFind {
		
		/**
		 * 跟据x,y坐标控件
		 * @param x
		 * @param y
		 */
		void findCell(int x , int y);
		
		
		
	}
	
}
